package com.bishe.tax.service;

import com.bishe.tax.entity.RegistrationInfo;
import com.bishe.tax.entity.SysUser;
import com.bishe.tax.mapper.RegistrationInfoMapper;
import com.bishe.tax.utils.Convert;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登记 服务层实现
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Service
public class RegistrationInfoService {
    @Autowired
    private RegistrationInfoMapper infoMapper;
    @Autowired
    IdentityService identityservice;
    @Autowired
    RuntimeService runtimeservice;
    @Autowired
    org.activiti.engine.TaskService taskservice;

    /**
     * 查询登记信息
     *
     * @param riId 登记ID
     * @return 登记信息
     */

    public RegistrationInfo selectInfoById(Integer riId) {
        return infoMapper.selectInfoById(riId);
    }

    /**
     * 查询登记列表
     *
     * @param info 登记信息
     * @return 登记集合
     */

    public List<RegistrationInfo> selectInfoList(RegistrationInfo info) {
        return infoMapper.selectInfoList(info);
    }

    /**
     * 新增登记
     *
     * @param info 登记信息
     * @return 结果
     */

    public int insertInfo(RegistrationInfo info) {
        info.setStatus(1);
        info.setRiType(1);
        return infoMapper.insertInfo(info);
    }

    /**
     * 修改登记
     *
     * @param info 登记信息
     * @return 结果
     */

    public int updateInfo(RegistrationInfo info) {
        return infoMapper.updateInfo(info);
    }

    /**
     * 删除登记对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */

    public int deleteInfoById(Integer id) {
        return infoMapper.deleteInfoById(id);
    }

    public int doSubmitFlow(SysUser user, String txId) {
        RegistrationInfo info = infoMapper.selectInfoById(Integer.valueOf(txId));
        Map<String, Object> variables = new HashMap<>();
        String creator = user.getUserId().toString();
        variables.put("creator", creator);
        variables.put("type", "registrationInfo");
        identityservice.setAuthenticatedUserId(user.getUserId().toString());
        ProcessInstance instance = runtimeservice.startProcessInstanceByKey("singleExamine", info.getRiId().toString(),
                variables);
        info.setProcessInstanceId(instance.getId());
        info.setStatus(2);
        return infoMapper.updateInfo(info);
    }

    public RegistrationInfo selectInfoBytaxReId(Integer taxReId) {
        return infoMapper.selectInfoBytaxReId(taxReId);
    }

}
